package hotgammon.domain;

public class StrategyMoveBetamon implements StrategyMove{

	public boolean move(Board board, Game game, Location from, Location to, Boolean validate) {
		Boolean validMove = true;
		
		//Can't move if it is not your turn
		if(!game.getColor(from).equals(game.getPlayerInTurn())){
			validMove = false;
		}
		//Can't move to a place with more than 1 opposite checker
		if(game.getCount(to)>1 && !(game.getColor(from).equals(game.getColor(to))) ){
			validMove = false;
		}
		//Can't move directly to bar
		if(to.equals(Location.R_BAR) || to.equals(Location.B_BAR)){
			validMove = false;
		}
		//NEEWWWWWWWWWWWWWWWWWWWWWWWWWWW!!!!!!!!!!!!
		//Can't move from bear_off ....funny error...test range of && and ||
		if(from.equals(Location.R_BEAR_OFF) || from.equals(Location.B_BEAR_OFF)){
			validMove = false;
		}
		//can only move the values the dices show
		if(!diceValuesContains(game.diceValuesLeft(),Math.abs(Location.distance(from, to)))){
			validMove = false;
		}
		//Bar movement...Can only play checker at bar
		if(game.getPlayerInTurn().equals(Color.RED) && (game.getCount(Location.R_BAR)>0) && !from.equals(Location.R_BAR)){
			validMove = false;
		}
		if(game.getPlayerInTurn().equals(Color.BLACK) && (game.getCount(Location.B_BAR)>0) && !from.equals(Location.B_BAR)){
			validMove = false;
		}
		//Correct direction movement
		if(game.getColor(from).equals(Color.BLACK) && Location.distance(from, to)<0){
			validMove = false;
		}
		if(game.getColor(from).equals(Color.RED) && Location.distance(from, to)>0){
			validMove = false;
		}
		
		
		//Bar movement...Beat checker to bar
		if(validMove && game.getCount(to)==1 && !(game.getColor(from).equals(game.getColor(to))) && game.getColor(from).equals(Color.RED )){
			if(!validate){
				board.move(to, Location.B_BAR);
				board.move(from, to);
				removeUsedDie(game, from, to, validate);
			}
			return true;
		}else if(validMove && game.getCount(to)==1 && !(game.getColor(from).equals(game.getColor(to))) && game.getColor(from).equals(Color.BLACK )){
			if(!validate){
				board.move(to, Location.R_BAR);
				board.move(from, to);
				removeUsedDie(game, from, to, validate);
			}
			return true;
		}
		
		if(validMove){
			//THIS METHOD HAS TO BE CALLED ON BAR MOVE AS WELL........
			if(!validate){
				removeUsedDie(game, from, to, validate);
				if(!validate)board.move(from, to);
			}
		}
		return validMove;
	}
	private boolean diceValuesContains(int[] values, int distance){
		for(int i = 0; i < values.length ; i++){
			if(values[i] == distance) return true;
		}
		return false;
	}
	
	private void removeUsedDie(Game game, Location from, Location to, boolean validate){
		//REMOVE LAST DICE EYE FROM DICEVALUES ARRAY//////////////////////////////////////
		int x = Math.abs(Location.distance(from, to));
		int[] diceValuesLeft = game.diceValuesLeft();
		int[] newLastDiceThrown = new int[diceValuesLeft.length-1];
		int j = 0;
		boolean foundX = false;
		for(int i = 0; i < diceValuesLeft.length; i++){
			if((diceValuesLeft[i] == x) && !foundX){
				foundX = true;
			}else if ((diceValuesLeft[i] == x) && foundX){
				newLastDiceThrown[j]=diceValuesLeft[i];
				j++;
			}else{
				newLastDiceThrown[j]=diceValuesLeft[i];
				j++;
			}
		}
		if(!validate)game.setDiceValuesLeft(newLastDiceThrown);
		//REMOVE LAST DICE EYE FROM DICEVALUES ARRAY//////////////////////////////////////
	}

}
